package com.eyalsh.data_structures.linkedLists.basic;

/**
 * @author Eyal Shalev
 * 
 */
public class DoublyLinkedListNode<T> extends LinkedListNode<T> implements
		com.eyalsh.data_structures.linkedLists.DoublyLinkedListNode<T> {

	private DoublyLinkedListNode<T> previous;

	/**
	 * Constructor for DoublyLinkedListNode<br>
	 * 
	 * @param data
	 */
	public DoublyLinkedListNode(T data, DoublyLinkedList<T> list) {
		super(data, list);
	}
	
	public DoublyLinkedList<T> list() {
		return (DoublyLinkedList<T>) super.list();
	}

	protected void previous(DoublyLinkedListNode<T> value) {
		previous = value;
	}

	public DoublyLinkedListNode<T> previous() {
		return previous;
	}
	
	@Override
	public DoublyLinkedListNode<T> next() {
		return (DoublyLinkedListNode<T>) super.next();
	}

	@Override
	public void append(T data) {
		DoublyLinkedListNode<T> temp = next();
		next(new DoublyLinkedListNode<T>(data, list()));
		next().next(temp);
		if (temp != null)
			temp.previous(next());
	}

	public void prepend(T data) {
		DoublyLinkedListNode<T> temp = previous();
		previous(new DoublyLinkedListNode<T>(data, list()));
		previous().previous(temp);
		if (temp != null)
			temp.next(previous());
	}

	@Override
	public boolean hasPrevious() {
		return previous() != null;
	}
}
